package com.c2dm.client;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class c2dmActivity extends Activity {
    /** Called when the activity is first created. */
    TextView t1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button b1=(Button)findViewById(R.id.button1);
        Button b2=(Button)findViewById(R.id.button2);
        Button b3=(Button)findViewById(R.id.button3);
        Button b4=(Button)findViewById(R.id.button4);
        t1=(TextView)findViewById(R.id.textView1);
        
        b1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	 Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
                 registrationIntent.putExtra("app", PendingIntent.getBroadcast(c2dmActivity.this, 0, new Intent(), 0));
            	 //registrationIntent.putExtra("app", "com.c2dm.client");
                 registrationIntent.putExtra("sender", "hung.pochin@gmail.com");
                 startService(registrationIntent);	
            }
        });
        
        b2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
            	unregIntent.putExtra("app", PendingIntent.getBroadcast(c2dmActivity.this, 0, new Intent(), 0));
            	startService(unregIntent);	
            }
        });
        
        b4.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	HttpClient hc = new DefaultHttpClient();  
            	HttpPost post = new HttpPost("http://stockprice2u.appspot.com/c2dm_server");  
            	try
            	{
            	List <NameValuePair> nvps = new ArrayList <NameValuePair>();  
            	nvps.add(new BasicNameValuePair("id", c2dmReceiver.id));  
            	post.setEntity(new UrlEncodedFormEntity(nvps));  
            	  
            	HttpResponse rp = hc.execute(post); 
            	}
            	catch(Exception e)
            	{}
            }
        });
        
        b3.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	c2dmActivity.this.finish();
            }
        });
    }
}